
package com.sun.showcase.biz.mall.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.SubjectClassifyDao;
import com.sun.showcase.biz.mall.service.SubjectClassifyService;
import com.sun.showcase.client.domain.mall.SubjectClassify;
import com.sun.showcase.client.query.mall.SubjectClassifyQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.UuidUtil;
@Service("subjectClassifyService")
@Transactional
public class SubjectClassifyServiceImpl implements SubjectClassifyService{
	@Autowired
	private SubjectClassifyDao subjectClassifyDao;
	
	public void setSubjectClassifyDao(SubjectClassifyDao dao) {
		this.subjectClassifyDao = dao;
	}

	public DataGrid datagrid(SubjectClassifyQuery subjectClassifyQuery) {
		DataGrid j = new DataGrid();
		Pager<SubjectClassify> pager  = find(subjectClassifyQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<SubjectClassifyQuery> getQuerysFromEntitys(List<SubjectClassify> subjectClassifys) {
		List<SubjectClassifyQuery> subjectClassifyQuerys = new ArrayList<SubjectClassifyQuery>();
		if (subjectClassifys != null && subjectClassifys.size() > 0) {
			for (SubjectClassify tb : subjectClassifys) {
				SubjectClassifyQuery b = new SubjectClassifyQuery();
				BeanUtils.copyProperties(tb, b);
				subjectClassifyQuerys.add(b);
			}
		}
		return subjectClassifyQuerys;
	}

	private Pager<SubjectClassify> find(SubjectClassifyQuery subjectClassifyQuery) {
		Page<SubjectClassify> page = PageHelper.startPage(subjectClassifyQuery.getPage().intValue(), subjectClassifyQuery.getRows().intValue());
		List<SubjectClassify> list = subjectClassifyDao.findList(subjectClassifyQuery);
		Pager<SubjectClassify> pager = new Pager<SubjectClassify>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public SubjectClassify add(SubjectClassifyQuery subjectClassifyQuery) {
		SubjectClassify t = new SubjectClassify();
		BeanUtils.copyProperties(subjectClassifyQuery, t);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		subjectClassifyDao.save(t);
		subjectClassifyQuery.setId(t.getId());
		return t;
		
	}

	public void update(SubjectClassifyQuery subjectClassifyQuery) {
		SubjectClassify t = subjectClassifyDao.getById(subjectClassifyQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(subjectClassifyQuery, t);
	    	subjectClassifyDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(SubjectClassifyQuery subjectClassifyQuery) {
	    subjectClassifyDao.updatePart(subjectClassifyQuery);
	}

	public void delete(java.lang.String[] ids) {
		subjectClassifyDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.String[] ids) {
		subjectClassifyDao.deletePt(ids);
	}

	public SubjectClassify get(SubjectClassifyQuery subjectClassifyQuery) {
		return subjectClassifyDao.getById(subjectClassifyQuery.getId());
	}

	public SubjectClassify get(String id) {
		return subjectClassifyDao.getById(id);
	}

	
	public List<SubjectClassifyQuery> listAll(SubjectClassifyQuery subjectClassifyQuery) {
	    List<SubjectClassify> list = subjectClassifyDao.findList(subjectClassifyQuery);
		List<SubjectClassifyQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}
	
	
}
